<?php
class Control_ActivityConf_shengxiao {
	function shengxiao() {
		$game 			= MooForm::request('game');
		$game = MooObj::get('Game')->transportGame($game);
		$server 		= MooForm::request('platform');
		$activityNameZh 		= MooForm::request('activity');
		$downLoadUrl 	= MooForm::request('downLoadUrl');
		$downLoadUrlLua 	= MooForm::request('downLoadUrlLua');
		$node_id 		= MooForm::request('node_id');
		
		$activityInfo = MooObj::get('ActivityConf')->getActivityInfoByZh($game, $activityNameZh);
		$activityId = $activityInfo['activityId'];
		
		$needLuaActivitys = MooConfig::get('activityconfig.needLuaActs');
		$needLua = false;
		if(in_array($activityNameZh, $needLuaActivitys)) {
			$needLua = true;
		}
		
		$nodeObj = MooDao::get('Node')->load($node_id);
		$xml_path = $nodeObj->xml_path;
		$fileNames = explode("/", $xml_path);
		$num = count($fileNames) - 1;
		$fileName = $fileNames[$num];
		
		$lua_path = $nodeObj->lua_path;
		$luaFileNames = explode("\\", $lua_path);
		$luaNum = count($luaFileNames) - 1;
// 		$luaFileName = $luaFileNames[$luaNum];
		
		$xmlName = explode(".xml", $fileName);
		$excelName = $xmlName[0] . ".xls";
		
		$luaId = $activityInfo['xmlLua'][$excelName]['luaId'] ? $activityInfo['xmlLua'][$excelName]['luaId'] : 1;
		
		$luaFileName = "Activity_{$activityId}_{$luaId}.lua";
// 		$luaFileName = "Activity_{$activityId}.lua";
		
		$servers  = MooConfig::get('activityconf_servers');
		$serverIp = $servers[$server];
		
		// xml生效
		$url = "http://{$serverIp}/xmleffect?params=name:{$fileName};address:" . base64_encode($downLoadUrl) . "&user=guest";
		$xmlRes = MooUtil::curl_send($url, null, 15);
		$xmlResult = MooJson::decode($xmlRes);
	
		if($needLua) {
			$luaUrl = "http://{$serverIp}/luaeffect?params=name:{$luaFileName};address:" . base64_encode($downLoadUrlLua) . "&user=guest";
			$luaRes = MooUtil::curl_send($luaUrl, null, 15);
			$luaRes = MooUtil::curl_send($luaUrl, null, 15);
			$luaResult = MooJson::decode($luaRes);
		}
		
		$date = date("Y-m-d");
		$log = ROOT_PATH . "/activityFiles/logs/" . "url.log." . $date;
		
		// 记录中文log
		$logUrl = urldecode($url);
		MooFile::write($log, date('Y-m-d H:i:s') ."--" . $downLoadUrl."\n", true);
		MooFile::write($log, date('Y-m-d H:i:s') ."--" . $logUrl."\n", true);
		if($needLua) {
			$logUrl = urldecode($luaUrl);
			MooFile::write($log, date('Y-m-d H:i:s') ."--" . $logUrl."\n", true);
		}
		
		if(!$xmlResult) {
			exit('{"status":"3"}');
		}
		
		if($needLua) {
			// lua和xml
			if($xmlResult['status'] == 1 && $luaResult['status'] == 1) {
				$res = '{"status":"1"}';
			} else {
				$res = '{"status":"2"}';
			}
		} else {
			// 只有xml
			$res = $xmlRes;
		}
		
		exit($res);
	}
}